package com.secsys.service.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.secsys.dto.RoleDTO;
import com.secsys.model.TbBbsModule;
import com.secsys.model.TbSysRole;
import com.secsys.service.RoleService;
/**
 * @Description  用户角色业务逻辑-实现类 
 * @author LiusQ
 *
 */

public class RoleServiceImpl extends ServiceImpl<TbSysRole,Integer> implements RoleService {

	@Override
	public void addRole(TbSysRole role) {
		// TODO Auto-generated method stub
		System.out.println("44444444");
		this.dao.save(role);
		
	}

	@Override
	public TbSysRole getBeanFromDto(RoleDTO roleDto) {
		// TODO Auto-generated method stub
		System.out.println("2222222222");
		TbSysRole role = new TbSysRole();
		role.setId(roleDto.getId());
		role.setRole(roleDto.getRole());
		role.setStatue(roleDto.getStatue());
		System.out.println(roleDto.getAttr().trim());
		TbBbsModule module=new TbBbsModule();
		module.setId(Integer.parseInt(roleDto.getAttr().trim()));
		role.setTbBbsModule(module);
		if(roleDto.getRemark()!=null){
			System.out.println("3333333333");
			role.setRemark(roleDto.getRemark());
		}
		return role;
	}

	@Override
	public List<RoleDTO> getDtoFromList(List<TbSysRole> tbRoleList) {
		// TODO Auto-generated method stub
		List<RoleDTO> roleList = new ArrayList<RoleDTO>();
		for(TbSysRole tbRole : tbRoleList){
			roleList.add(getDtoFromPojo(tbRole));
		}
		return roleList;
	}

	@Override
	public RoleDTO getDtoFromPojo(TbSysRole tbRole) {
		// TODO Auto-generated method stub
		RoleDTO roleDto = new RoleDTO();
		roleDto.setId(tbRole.getId());
		roleDto.setRole(tbRole.getRole());
		roleDto.setStatue(tbRole.getStatue());
		roleDto.setRemark(tbRole.getRemark());
		return roleDto;
	}

	@Override
	public List<RoleDTO> listRole() {
		// TODO Auto-generated method stub
		List<RoleDTO> roleList = getDtoFromList(this.dao.find("from TbSysRole"));
		
		return roleList;
	}

	@Override
	public TbSysRole findTbSysRoleByName(String role) {
		// TODO Auto-generated method stub
		TbSysRole sysRole = new TbSysRole();
		List<TbSysRole> roleList = this.dao.findByProperty("from TbSysRole where role=?",role);
		if(roleList!=null && roleList.size()>0){
			sysRole = roleList.get(0);
		}else{
			return null;
		}
		return sysRole;
	}
      /**
       * 
       * @Description query Module Manager Role 
       * @param moduleId
       * @param statue
       * @return
       */
   public  TbSysRole queryTbSysRoleByModuleId(Integer moduleId,String statue){
	    String hql="from TbSysRole where tbBbsModule.id=? and statue=?";
	    List   roleList=dao.find(hql, new Object[]{moduleId,statue});   
	     return roleList!=null&&roleList.size()>0?(TbSysRole)roleList.get(0):null;
	   
   }

	@Override
	public TbSysRole findTbSysRoleByName(String role, Integer id) {
		// TODO Auto-generated method stub
		TbSysRole sysRole = new TbSysRole();
		List<TbSysRole> roleList = this.dao.find("from TbSysRole where role=? and id!=?",new Object[]{role,id});
		if(roleList!=null && roleList.size()>0){
			sysRole = roleList.get(0);
		}else{
			return null;
		}
		return sysRole;
	}

}
